package jfood.chef;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import morfologik.stemming.PolishStemmer;
import morfologik.stemming.WordData;

public class Stemmer {
	public class Stem
	{
		public Stem(String word, String stem, String tags)
		{
			stem_ = stem;
			tags_ = tags;
			word_ = word;
		}
		String getStem()
		{
			return stem_;
		}
		String getTags()
		{
			return tags_;
		}
		String getWord()
		{
			return word_;
		}
		
		void setStem(String stem)
		{
			stem_ = stem;
		}
		void setTags(String tags)
		{
			tags_ = tags;
		}
		void setWord(String word)
		{
			word_ = word;
		}
		
		public String toString()
		{
			String ret =
					"Word: "+ word_+ "\n" +
					"Tags: "+ tags_+ "\n" +
					"Stem: "+ stem_+ "\n";
			return ret;
		}
		void print()
		{
			
		}
		
//		public boolean isProper()
//		{
//			final String COLL_DELIM = "\\+";
//
//			String collections[] = tags_.split(COLL_DELIM);
//			boolean retValue = false;
//			for(String c: collections)
//			{
//				if(hasProperTag(c))
//				{
//					retValue = true;
//					break;
//				}
//			}
//			return retValue;
//		}
		
//		private boolean hasForbiddenTag(String collection)
//		{
//			final String BREV_TAG = "brev";
//			final String TAGS_DELIM = ":";	
//			
//			String tags[] = collection.split(TAGS_DELIM);
//			
//			boolean retValue = false;
//			for(String tag: tags)
//			{
//				if(tag.equals(BREV_TAG))
//				{
//					retValue = true;
//					break;
//				}
//			}
//			return retValue;
//		}
		
		private String tags_;
		private String stem_;
		private String word_;
	}
	
	public Stemmer(){
		stemmer_ = new PolishStemmer();
		forbiddenStems_ = getListFromFile("forbidden_stems.txt");
		forbiddenTags_ = getListFromFile("forbidden_tags.txt");
	}
	private ArrayList<String> getListFromFile(String path)
	{
		ArrayList<String> list = new ArrayList<String>();
		
		File file = new File(path);
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(file));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		String line = null;
		while(true)
		{
			try {
				line = reader.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			if(line == null)
			{
				break;
			}
			else
			{
				list.add(line);
			}
		}
		try {
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return list;
	}
	private ArrayList<String> getTokens(String sentence)
	{
		String[] splittedSentece = sentence.split("[ ~@#$^&*()+=\\[\\]{}|\\\\,.?:;,\"]");
		
		ArrayList<String> returnList = new ArrayList<String>();
		for(int i = 0; i<splittedSentece.length;++i)
		{
			String word = splittedSentece[i];
			if(word.isEmpty() == false)
			{
				returnList.add(splittedSentece[i]);
			}
		}
		return returnList;
	}

	private ArrayList<Stem> stem(ArrayList<String> words)
	{
		ArrayList<Stem> stems = new ArrayList<Stem>();
		for(String word: words)
		{
			stems.addAll(stem(word));
		}
		return stems;
	}	
	private ArrayList<Stem> stem(String word) {
        
		ArrayList<Stem> stems = new ArrayList<Stem>();
		List<WordData> wordsDataList = stemmer_.lookup(word);
		String lastStem = "";

		if (wordsDataList.isEmpty()) {
			Stem stem = new Stem(word, word, "unknown");
			stems.add(stem);
		}
		for (WordData wd : wordsDataList) {
			String s = wd.getStem().toString();
			if (s.equals(lastStem) == false) {
				String tag = wd.getTag().toString();
				Stem stem = new Stem(word, s, tag);
				stems.add(stem);
				lastStem = s;
			}
		}
		return stems;
    }
	
	public ArrayList<Stem> stemSentence(String sentence, boolean deleteNumbers)
	{
		sentence = sentence.replaceAll("[(,),\\-,/]", "");
		if(deleteNumbers == true)
		{
			sentence = sentence.replaceAll("[0-9]","");
		}
		
		ArrayList<String> tokens = getTokens(sentence);
		
		ArrayList<Stem> stems = stem(tokens);
		return stems;
	}
	public ArrayList<Stem> stemSentenceWithForbiddens(String sentence, boolean deleteNumbers)
	{
		String lowerCaseSenstence = sentence.toLowerCase();
		ArrayList<Stem> stems = stemSentence(lowerCaseSenstence, deleteNumbers);
		
		stems = getNonForbiddenStems(stems);
		
		return stems;
	}
	
	private boolean isForbiddenStemName(Stem stem)
	{
		boolean retValue = false;
		for(String s: forbiddenStems_)
		{
			if(s.equals(stem.getStem()))
			{
				retValue = true;
				break;
			}
		}
		return retValue;
	}
	private boolean isForbiddenTag(Stem stem)
	{
		boolean retValue = false;
		final String TAGS_DELIM = "[:,\\+]";	
		
		String[] tags = stem.getTags().split(TAGS_DELIM);
		for(String t: forbiddenTags_)
		{
			if(tags[0].equals(t))
			{
				retValue = true;
				break;
			}
		}
		return retValue;
	}
	
	public ArrayList<Stem> getNonForbiddenStems(ArrayList<Stem> stems)
	{
		ArrayList<Stem> nonForbiddenStems = new ArrayList<Stem>();
		
		for(Stem s: stems)
		{
			final boolean isForbiddenTag = isForbiddenTag(s);
			if(!isForbiddenTag)
			{
				final boolean isForbiddenStemName = isForbiddenStemName(s);
				if(!isForbiddenStemName)
				{
					nonForbiddenStems.add(s);
				}
			}
		}
		return nonForbiddenStems;
	}
    public static void main(String[] argv)
    {
    	Stemmer stemmer = new Stemmer();
    	Scanner sc = new Scanner(System.in);
		String line;
		while (sc.hasNextLine()) {
			line = sc.nextLine();
			ArrayList<Stem> stems = stemmer.stemSentence(line,false);
			ArrayList<Stem> nonForbiddenStems = stemmer.getNonForbiddenStems(stems);
			
			for(Stem s: stems)
			{
				System.out.println(s.toString());
			}
			
			for (Stem s: nonForbiddenStems)
			{
				System.out.print(s.getStem());
				System.out.print(" ");
			}
			
		}
		sc.close();
    }
    /* */
    
    private ArrayList<String> forbiddenStems_;
    private ArrayList<String> forbiddenTags_;
    private PolishStemmer stemmer_;
}
